#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 2e5 + 10;

ll pre[N][25];
int t, l, r;

void init() {
    for (int i = 0; i < 25; i++) {
        for (int j = 1; j < N; j++) {
            if ((j & (1 << i)) == 0)
                pre[j][i] = 1;
            else
                pre[j][i] = 0;
        }

        for (int j = 1; j < N; j++)
            pre[j][i] += pre[j - 1][i];
    }
}
int main() {
    init();
    cin >> t;
    while (t--) {
        cin >> l >> r;
        ll res = 1e9;
        for (int i = 0; i < 25; i++) {
            res = min(res, pre[r][i] - pre[l - 1][i]);
        }
        cout << res << endl;
    }

    return 0;
}